<?php
/**
 *
 *
 */
class PageBaseUpdate extends PageBase{
    private $_action;
    private $_id;
    private $_className;
    private $_localControls             =   null;
    private $_optionalUpdateControls    =   null;

    public function onInit($param){
        parent::onInit($param);

        if(!$this->IsPostBack){
            $this->LoadDataRelated();
            $this->ResolveAction();
        }
    }

    /**
     * PublicidadUpdate::ResolveAction()
     *
     * @return
     */
    public function ResolveAction()
    {
        if($this->_action  ==   ActionType::Update){
            $objectData =   new $this->_className();
            $objectData =   $objectData->getPeer()->retrieveByPK($this->_id);
            $this->MapControls($objectData);
        }
    }

    protected function getLocalControls(){
        if(is_null($this->_localControls)){
            $this->_localControls   =   new TList();
        }

        return $this->_localControls;
    }

    protected function setClassName($value){
       $this->_className    =   TPropertyValue::ensureString($value);
    }

    protected function setLocalControls($value){
        if(is_string($value)){
            $value  =   split(",", $value);
        }

        if(is_array($value)){
            $this->LocalControls->mergeWith($value);
        }
    }

    protected function getOptionalUpdateControls(){
        if(is_null($this->_optionalUpdateControls)){
            $this->_optionalUpdateControls   =   new TMap();
        }

        return $this->_optionalUpdateControls;
    }

    protected function setOptionalUpdateControls($value){
        if(is_string($value)){
            $value  =   split(",", $value);
        }

        if(is_array($value)){
            for($i = 0; $i < count($value); $i+=2){
                $this->OptionalUpdateControls->add($value[$i], $value[$i + 1]);
            }
        }
    }

    protected function LoadDataRelated(){

    }

    public function InitializeComponent()
    {
        parent::InitializeComponent();
        $this->_id  =   $this->Request["id"];
        $this->_action  =   ActionType::Insert;

        if(!is_null($this->_id)){
            $this->_action  =   ActionType::Update;
        }
    }

    /**
     * PublicidadUpdate::MapControls()
     *
     * @param mixed $banner
     * @return
     */
    public function MapControls($data)
    {
        foreach($this->LocalControls as $control){
            $getter =   "get" . $control;

            if(method_exists($data, $getter)){
                switch(get_class($this->$control)){
                    case "TCheckBox":
                        $this->$control->Checked   =   $data->$getter();
                        break;

                    case "THiddenField":
                        $this->$control->Value      =   $data->$getter();
                        break;

                    default:
                        $this->$control->Text      =   $data->$getter();
                        break;
                }
            }
        }
    }

    /**
     * PublicidadUpdate::Save()
     *
     * @return
     */
    protected function Save()
    {
        $dataObject =   new $this->_className();

        if($this->_action == ActionType::Update){
            $dataObject =   $dataObject->getPeer()->retrieveByPK($this->_id);
        }

        $this->MapFields($dataObject);
        $dataObject->save();

        return $dataObject->getPrimaryKey();
    }

    /**
     * PublicidadUpdate::MapFields()
     *
     * @param mixed $banner
     * @return
     */
    protected function MapFields(&$objectData)
    {
        foreach($this->LocalControls as $control){
            $setter =   "set" . $control;

            if(method_exists($objectData, $setter)){
                switch(get_class($this->$control)){
                    case "TCheckBox":
                        $value  =   $this->$control->Checked;
                        //$data->$setter($this->$control->Checked);
                        break;

                    case "THiddenField":
                        $value  =   $this->$control->Value;
                        break;

                    default:
                        $value  =   $this->$control->Text;
                        //$data->$setter($this->$control->Text);
                        break;
                }

                $hasSetValue    =   true;

                if($this->_action == ActionType::Update){
                    if($this->OptionalUpdateControls->offsetExists($control)){
                        if($this->OptionalUpdateControls[$control] == $value){
                            $hasSetValue    =   false;
                        }
                    }
                }

                if($hasSetValue){
                    $objectData->$setter($value);
                }
            }
        }
    }
}

/**
 *
 *
 */
class ActionType extends TEnumerable{
    const Insert  =   "Insert";
    const Update  =   "Update";
    const Delete  =   "Delete";

}
?>